package com.salton123.imagepicker.bean;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;

import androidx.annotation.NonNull;

/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧 Github地址：https://github.com/jeasonlzy0216
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：图片信息
 * 修订历史：
 * ================================================
 */
public class ImageItem implements Serializable, Parcelable, Comparable<ImageItem> {

    public String name = "";       //图片的名字
    public String path = "";       //图片的路径
    public long size = 0;         //图片的大小，KB
    public int width = 0;         //图片的宽度, px
    public int height = 0;        //图片的高度, px
    public String mimeType = "";   //图片的类型
    public long addTime = 0;      //图片的创建时间

    public long duration;     //视频时长

    public int type = TYPE_IMAGE;    //图片类型
    public static final int TYPE_ADD = 0x10;
    public static final int TYPE_IMAGE = 0x11;

    public ImageItem(int type, String path) {
        this.type = type;
        this.path = path;
    }

    /**
     * 图片的路径和名字相同就认为是同一张图片
     */
    @Override
    public boolean equals(Object o) {
        try {
            if (o instanceof ImageItem) {
                ImageItem item = (ImageItem) o;
                return this.path.equalsIgnoreCase(item.path) && this.name.equals(item.name);
            }
        } catch (Exception e) {
            return super.equals(o);
        }
        return super.equals(o);
    }

    @Override
    public int hashCode() {
        return 0;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeString(this.path);
        dest.writeLong(this.size);
        dest.writeInt(this.width);
        dest.writeInt(this.height);
        dest.writeString(this.mimeType);
        dest.writeLong(this.addTime);
        dest.writeLong(this.duration);
    }

    public ImageItem() {
    }

    protected ImageItem(Parcel in) {
        this.name = in.readString();
        this.path = in.readString();
        this.size = in.readLong();
        this.width = in.readInt();
        this.height = in.readInt();
        this.mimeType = in.readString();
        this.addTime = in.readLong();
        this.duration = in.readLong();
    }

    public static final Creator<ImageItem> CREATOR = new Creator<ImageItem>() {
        @Override
        public ImageItem createFromParcel(Parcel source) {
            return new ImageItem(source);
        }

        @Override
        public ImageItem[] newArray(int size) {
            return new ImageItem[size];
        }
    };

    @NonNull
    @Override
    public String toString() {
        return "ImageItem:name = " + name
            + ",size = " + size
            + ",width = " + width
            + ",height = " + height
            + ",mimeType = " + mimeType
//                + "，addTime = " + addTime
            + ",path = " + path;
    }

    @Override
    public int compareTo(@NonNull ImageItem o) {
        return Long.compare(o.addTime, this.addTime);
    }

}
